package requestCase

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object AvgFromDataAgentLog {

  def main(args: Array[String]): Unit = {

    val conf = new SparkConf().setMaster("local[*]").setAppName("AvgFromDataAgentLog...")
    val context = new SparkContext(conf)

    val text = context.textFile("D:\\IDEA_prjects\\sparkDemo\\src\\main\\scala\\data\\avgtest.txt").cache()

    val valueOne: RDD[(Int, Int)] = text.flatMap(_.split(" ")).map((_.toInt)).map((_, 1))
    val tuple: (Int, Int) = valueOne.reduce((x, y) => ((x._1 + y._1), (y._2 + x._2)))
    println(tuple._1/tuple._2)

    val count = text.count()
    val value = text.map(_.split(" ")(1).toInt)
    val sum = value.reduce(_ + _)
    val i = value.reduce((x, y) => x + y)
    println(sum/count)
    println(i/count)
  }
}
